<template>
  <div>
    <div>参考网址：https://www.makeapie.com/editor.html?c=xo2-c3fuFe</div>
    <div style="height: 400px;position: relative;">
      <div :id="moduleName" class="box"></div>
      <div class="xLabel xLabel2" ref="xLable">
        <div class="xText">
          <div v-for="(item,index) in xAxisData" :key="index">{{item}}</div>
        </div>
        <div class="TText">
          <div v-for="(item,index) in titleText" :key="index">{{item}}</div>
        </div>
      </div>
    </div>
  </div>

</template>
<script>
export default {
  name: 'ColumnChart',
  props: {
    // 图表名称
    moduleName: {
      type: String
    },
    // 底部标题离左面位置
    location: {
      type: Array
    },
    // 底部标题大小
    titleSize: {
      type: Number,
      default: 16
    }
  },
  data () {
    return {
      // 柱状图x轴数据
      xAxisData: ['风电', '光伏', '风电', '光伏', '风电', '光伏'],
      // 柱状图x轴数据标题
      titleText: ['年有效期合格率', '工具种类合格率', '工具种类合格率']
    }
  },
  mounted () {
    this.drawLine()
  },
  methods: {
    drawLine () {
      // 基于准备好的dom，初始化echarts实例
      const myChart = this.$echarts.init(document.getElementById(this.moduleName))
      let arr = [];
      let list = [];
      let series = [];
      let legend = [];
      let fontSize=16;
      let barWidth=10;
      let barGap=100;
      // let maxList=[];
      // let max=460;//背景最大值取y轴刻度线（最后注释部分）
      let colorStartList=["transparent",'transparent','transparent','transparent']
      let colorLeftList=["#08a2e9","#09c8ed","#2f70ff","#37d790"]
      let colorTopList = ["#82d4f9",'#6ff8f9','#7aa2fe','#5ff4b3']
      // let colorTopList= [
      //   {
      //     beginColor: 'rgba(30,126,255,0.5)',
      //     endColor: 'rgba(30,126,255,1)'
      //   },
      //   {
      //     beginColor: 'rgba(255,167,90,0.5)',
      //     endColor: 'rgba(255,167,90,1)'
      //   },
      //   {
      //     beginColor: 'rgba(106,204,41,0.5)',
      //     endColor: 'rgba(106,204,41,1)'
      //   },
      //   {
      //     beginColor: 'rgba(152,51,255,0.5)',
      //     endColor: 'rgba(152,51,255,1)'
      //   }
      // ]
      let colorEndList=["#51c5fa",'#64d8ee','#6d98f9','#5ddea6']
      let chartObj={
        series:[
          {name:"当前资源",data:[26, 43, 13, 40, 15, 11]},
          {name:"其他资源",data:[26, 43, 13, 40, 15, 11]},
          {name:"资源",data:[6, 9, 8, 7, 7, 6]},
          {name:"资源1",data:[5, 15, 12, 8, 5, 6]}
        ],
        chartList:['风电', '光伏', '风电', '光伏', '风电', '光伏']
      }
      function colorRgba(str,alpha){
        let reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
        var sColor = str.toLowerCase();
        if(sColor=='transparent'){
          return 'transparent'
        }
        if(sColor && reg.test(sColor)){
          if(sColor.length === 4){
            var sColorNew = "#";
            for(let i=1; i<4; i+=1){
              sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));
            }
            sColor = sColorNew;
          }
          //处理六位的颜色值
          var sColorChange = [];
          for(let i=1; i<7; i+=2){
            sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));
          }
          console.log('颜色',"rgba(" + sColorChange.join(",") +','+alpha+ ")")
          return "rgba(" + sColorChange.join(",") +','+alpha+ ")";
        }else{
          return sColor;
        }
      }
      if (chartObj.series && chartObj.series.length) {
        chartObj.series.forEach((ele) => {
          arr = arr.concat(ele.data);
        });
        list = chartObj.series[0].data.map(() => {
          return 1;
        });
        chartObj.series.forEach((item, index) => {
          legend.push({
            name: item.name,
            itemStyle: {
              color: colorEndList[index % colorEndList.length],
            },
          });
          //底部切片
          series.push({
            data: list,
            color: colorStartList[index % colorStartList.length],
            type: 'pictorialBar',
            tooltip: {
              show: false,
            },
            barMaxWidth: fontSize * 3,
            symbol: 'diamond',
            symbolOffset: [
              (-0.5 * (chartObj.series.length - 1) +
                index +
                -0.5 * barGap * 0.01 * (chartObj.series.length - 1) +
                barGap * 0.01 * index) *
              barWidth,
              '50%',
            ],
            symbolSize: [barWidth, barWidth * 0.5],
          });

          //实际数据顶部切片
          series.push({
            data: item.data,
            type: 'pictorialBar',
            tooltip: {
              show: false,
            },
            barMaxWidth: fontSize * 3,
            color: colorTopList[index],
            // color: new this.$echarts.graphic.LinearGradient(1, 0, 0, 0, [{
            //   offset: 0,
            //   color: colorTopList[index].endColor
            // }, {
            //   offset: 1,
            //   color: colorTopList[index].beginColor
            // }], false), // 渐变,
            symbolPosition: 'end',
            symbol: 'diamond',
            symbolOffset: [
              (-0.5 * (chartObj.series.length - 1) +
                index +
                -0.5 * barGap * 0.01 * (chartObj.series.length - 1) +
                barGap * 0.01 * index) *
              barWidth,
              '-50%',
            ],
            symbolSize: [barWidth, barWidth * 0.4],
            zlevel: 2,
          });
          //实际数据侧边切片
          series.push({
            data: item.data,
            type: 'pictorialBar',
            tooltip: {
              show: false,
            },
            barMaxWidth: fontSize * 3,
            color: {
              x: 0,
              y: 1,
              x2: 0,
              y2: 0,
              type: 'linear',
              global: false,
              colorStops: [
                {
                  offset: 0,
                  color: 'rgba(255,255,255,0.3)',
                },
                {
                  offset: 1,
                  color: colorRgba(colorLeftList[index % colorLeftList.length], 1),
                },
              ],
            },
            symbolPosition: 'end',
            symbol: 'rect',
            symbolSize: [barWidth / 2, '100%'],
            symbolOffset: [
              (-0.5 * (chartObj.series.length - 1) +
                index +
                -0.5 * barGap * 0.01 * (chartObj.series.length - 1) +
                barGap * 0.01 * index -
                0.25) *
              barWidth,
              0,
            ],
            zlevel: 1,
          });
          //柱子
          series.push(
            {
              data: item.data,
              type: 'bar',
              name: item.name,
              barGap: barGap + '%',
              barWidth: barWidth,
              barMaxWidth: fontSize * 3,
              label: {
                show: true,
                position: 'top',
                distance: fontSize * 0.3,
                textStyle: {
                  color: colorEndList[index % colorEndList.length],
                  fontSize: fontSize,
                },
              },
              itemStyle: {
                color: {
                  x: 0,
                  y: 1,
                  x2: 0,
                  y2: 0,
                  type: 'linear',
                  global: false,
                  colorStops: [
                    {
                      offset: 0,
                      color: 'rgba(255,255,255,0.3)',
                    },
                    {
                      offset: 1,
                      color: colorRgba(colorEndList[index % colorEndList.length], 1),
                    },
                  ],
                },
              },
            });
        });
      }
      let option = {
        title: {
          text: '',
          top: 20,
          left: 'center',
          textStyle: {
            color: '#fff',
            fontSize: 20,
          },
        },
        tooltip: {
          show: true,
          trigger: 'axis',
          backgroundColor: 'rgba(116,176,222,0.3)',
          extraCssText: 'box-shadow: 0 0 8px rgba(0, 128, 255, 0.27) inset;',
          borderWidth: 0,
          confine: false,
          appendToBody: true,
          formatter: (params) => {
            ////console.log(params,'---paramsshopping')
            let result = "<div style='margin-bottom:5px;'>" + params[0].name + '</div>';
            params.forEach((item, index) => {
              var dotHtml = `<span style="display:inline-block;margin-right:5px;border-radius:50%;width:${fontSize}px;height:${fontSize}px;background:${colorEndList[index]}"></span>`;
              result +=
                "<div style='display:flex;align-items:center;margin-bottom:5px'>" +
                dotHtml +
                item.seriesName +
                (item.seriesName ? "<span style='display:inline-block;margin-right:6px;'></span>" : '') +
                item.data +
                '</div>';
            });
            return result;
          },
          //轴触发提示才有效
          axisPointer: {
            type: 'shadow',
            shadowStyle: {
              color: 'rgba(35,49,77,0.3)',
            },
          },
          textStyle: {
            color: '#fff',
            fontSize: fontSize,
          },
        },
        grid: {
          top: fontSize * 3.5,
          left: 5,
          right: 5,
          bottom: fontSize,
          containLabel: true, //包括文本，居中
        },
        legend: {
          show: true,
          data: legend,
          selectedMode: false, //图例是否可以点击
          type: 'scroll',
          icon: 'circle',
          itemWidth: fontSize,
          itemHeight: fontSize,
          textStyle: {
            color: '#C3E3F9',
            fontSize: fontSize,
          },
        },
        xAxis: {
          data: chartObj.chartList,
          type: 'category',
          axisLine: {
            show: true,
            lineStyle: {
              color: '#2B72A1',
            },
          },
          axisLabel: {
            fontSize: fontSize,
            color: '#87CCFF',
          },
          splitLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          interval: 0,
        },
        yAxis: [
          {
            type: 'value',
            name: '单位',
            nameGap: fontSize * 1.5,
            nameTextStyle: {
              align: 'center',
              fontSize: fontSize,
              color: '#2869A9',
            },
            splitNumber: 4,
            splitLine: {
              show: true,
              lineStyle: {
                color: 'rgba(255,255,255,0.1)',
              },
            },
            axisLine: {
              show: false,
            },
            axisLabel: {
              margin: 10,
              fontSize: fontSize,
              color: '#2869A9',
            },
            axisTick: {
              show: true,
              lineStyle: {
                color: '#2869A9',
              },
            },
          },
        ],
        series: series

      };
      // 绘制图表
      myChart.setOption(option)
      let gridWid = myChart.getWidth()
      document.querySelector('.xLabel2').style.width=(gridWid - 50) + 'px'
      window.addEventListener('resize', () => {
        myChart.resize()
        let gridWid = myChart.getWidth()
        document.querySelector('.xLabel2').style.width=(gridWid - 50) + 'px'
      })
    }

  }
}
</script>
<style scoped lang="less">
.box {
  height: 100%;
  width: 100%;
  position: absolute;
}
  .xLabel{
    width: calc(100% - 110px);
    position: absolute;
    bottom: -48px;
    left: 40px;
    &>div{
      width: 100%;
      display: flex;
      box-sizing: border-box;
      div{
        flex-grow: 1;
        text-align: center;
        background-color: #eff4fb;
      }
    }
    .xText {
      div:nth-child(odd) {
        border-radius: 10px 0 0 0;
        padding: 10px 0;
        position:relative;
        margin-left: 5px;
        /*border-right: 1px solid #fff*/
        &::after{
          content: '';
          position: absolute;
          bottom: 10px;
          /*left: 0;*/
          right: 0;
          height: 50%;
          border-right: 1px solid #fff;
          border-top-width: 0;
        }
      }
      div:nth-child(even) {
        border-radius: 0 10px 0 0 ;
        padding: 10px 0;
        margin-right: 5px;
      }
    }
    .TText{
      div{
        border-radius: 0 0 10px 10px;
        padding: 10px 0;
        position: relative;
        margin: 0 5px;
        &::after{
          content: '';
          position: absolute;
          top: 0;
          left: 10%;
          width: 80%;
          border-top: 1px solid #fff;
        }
      }
    }
  }

</style>
<style>
  .highcharts-point{
    rx: 0;
    ry: 0
  }
</style>
